package com.micro.modual.controller;

import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.model.bo.login.LoginBO;
import com.micro.modual.model.dto.token.TokenDTO;
import com.micro.modual.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <h1>登录授权</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/4/25
 */
@Api(tags = "登录授权")
@RestController
@RequestMapping(value = "/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * <h2>登陆操作 获取token信息</h2>
     * @param loginBO {@link LoginBO} 登录请求对象
     * @param request  {@link HttpServletRequest} 用于在生成token时候获取请求头信息,区分请求客户端
     * @return {@link TokenDTO}  登录请求返回对象
     * @throws Exception
     */
    @ApiOperation(value = "登录-获取授权",notes = "客户统一授权接口")
    @PostMapping(value = "/createAuthenticationToken")
    public RespJsonData<TokenDTO> createAuthenticationToken(@RequestBody LoginBO loginBO, HttpServletRequest request) throws Exception {
        return loginService.createAuthenticationToken(loginBO,request);
    }

    /**
     * <h2>刷新token</h2>
     * @param customerCode  客户编号
     * @param request       {@link HttpServletRequest} 用于在生成token时候获取请求头信息,区分请求客户端
     * @return  {@link TokenDTO}  登录请求返回对象
     * @throws Exception
     */
    @ApiOperation(value = "刷新token",notes = "刷新accessToken接口")
    @PostMapping(value = "/refreshToken")
    public RespJsonData<TokenDTO> refreshToken(String customerCode,HttpServletRequest request) throws Exception {
        return loginService.refreshToken(customerCode,request);
    }

    /**
     * <h2>校验token有效性</h2>
     * @param token 请求token
     * @return
     */
    @PostMapping(value = "/validataToken/{token}")
    public RespJson validataRequestToken(@PathVariable("token") String token) throws Exception {
        return loginService.validataRequestToken(token);
    }

    /**
     * <h2>账户退出</h2>
     * @return {@link RespJson} 返回结果基础类
     */
    @ApiOperation(value = "账户退出",notes = "账户退出")
    @PostMapping(value = "/logout")
    public RespJson logout(HttpServletRequest request){
        return loginService.logout(request);
    }

}
